package com.xiaoxin.errorDemo.error;

import lombok.Getter;

@Getter
public enum ErrorEnum {
    ID_NOT_NULL("F0001", "主键ID不能为空", false),
    // ...
    UNKNOWN("U0001", "未知异常，请联系管理员", false);
    private String code;
    private String message;
    private boolean canRetry;

    ErrorEnum(String code, String message, boolean canRetry) {
        this.code = code;
        this.message = message;
        this.canRetry = canRetry;
    }

    /**
     * 根据Code 获取错误枚举
     *
     * @param code Code
     * @return 错误枚举
     */
    public static ErrorEnum getByCode(String code) {
        for (ErrorEnum errorEnum : ErrorEnum.values()) {
            if (errorEnum.code.equals(code)) {
                return errorEnum;
            }
        }
        return UNKNOWN;
    }
}
